<?php

namespace app\common\dao\system;

use app\common\model\system\SystemConfig as model;
use app\common\dao\BaseDao;

class SystemConfigDao extends BaseDao
{
    /**
    * 设置模型
    * @return model
    */
    protected function getModel(): string
    {
        return model::class;
    }

    public function classifyIdExists(int $classify_id)
    {
        return $this->fieldExists('config_classify_id', $classify_id);
    }


    public function keyExists($key, ?int $except = null): bool
    {
        return $this->fieldExists('config_key', $key, $except);
    }


    public function search(array $where)
    {
        $query = ($this->getModel())::getDB();
        if ($where['keyword'])
            $query->where('config_name|config_key', 'like','%' . $where['keyword'] . '%');
        if (isset($where['pid']) && $where['pid'] !== '') $query->where('config_classify_id', $where['pid']);
        if (isset($where['config_classify_id']) && $where['config_classify_id']!=='')
            $query->where('config_classify_id', $where['config_classify_id']);
        return $query;
    }


    public function cidByConfig(int $cid, int $user_type)
    {
        return ($this->getModel())::getDB()->where('config_classify_id', $cid)->where('user_type', $user_type)->where('status', 1)->select();
    }


    public function intersectionKey(int $cid, $keys): array
    {
        return ($this->getModel())::where('config_classify_id', $cid)->whereIn('config_key', $keys)->where('status', 1)->column('config_type,config_name', 'config_key');
    }


}